{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "15aac7fe-663e-4d61-906c-bd99f61ad610",
   "metadata": {},
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'numpy'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mModuleNotFoundError\u001b[0m                       Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[1], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01mnumpy\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mas\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01mnp\u001b[39;00m\n\u001b[0;32m      2\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01mpandas\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mas\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01mpd\u001b[39;00m\n\u001b[0;32m      3\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01mmatplotlib\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mpyplot\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mas\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01mplt\u001b[39;00m\n",
      "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'numpy'"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import os\n",
    "import pandas\n",
    "data = pd.read_excel(r'D:\\date_1.xlsx')\n",
    "display(data.sample(5))\n",
    "y = data['amount']\n",
    "X = data.drop(['ts_code','trade_date','the_date'],axis = 1)\n",
    "\n",
    "from sklearn.linear_model import LinearRegression\n",
    "simple2 = LinearRegression()\n",
    "from sklearn.model_selection import train_test_split\n",
    "x_train,x_test,y_train,y_test = train_test_split(x,y,random_state=666)\n",
    "simple2.fit(x_train,y_train)\n",
    "print('多元线性回归模型系数：\\n',simple2.coef_)\n",
    "print('多元线性回归模型常数项：',simple2.intercept_)\n",
    "y_predict = simple2.predict(x_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a5c40e05-ea62-44e6-8950-7cfb37af95d4",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.metrics import mean_absolute_error \n",
    "from sklearn.metrics import mean_squared_error \n",
    "from sklearn.metrics import r2_score\n",
    "#直接调用库函数输出R2\n",
    "print('预测值的均方误差：',\n",
    "mean_squared_error(y_test, y_predict))\n",
    "print(r2_score(y_test, y_predict))\n",
    "print(simple2.score(x_test, y_test))\n",
    "print('各特征间的系数矩阵：\\n', simple2.coef_)\n",
    "print('影响房价的特征排序：\\n', np.argsort(simple2.coef_))\n",
    "print('影响房价的特征排序：\\n',\n",
    "X.columns[np.argsort(simple2.coef_)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6974e6ca-4b07-4aa3-aa36-52595bc5134c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8rc1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
